package net.huashitong.synthetical.newSupervision.service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.dao.SysDeptDao;
import com.sirdc.modules.sys.dao.SysTableDao;
import com.sirdc.modules.sys.dao.SysUserDao;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.SysTableService;
import com.sirdc.modules.sys.util.SysUserUtils;
import net.huashitong.synthetical.newSupervision.dao.TaskBackDao;
import net.huashitong.synthetical.newSupervision.dao.TaskNewDao;
import net.huashitong.synthetical.newSupervision.dao.TaskSendDao;
import net.huashitong.synthetical.newSupervision.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/7/16
 * Time: 12:22
 * To change this template use File | Settings | File Templates.
 * Description:
 */
@Service
public class TaskBackService extends StringPKService<TaskBack> {
    @Autowired
    private TaskBackDao taskBackDao;
    @Autowired
    private SysTableService tableService;
    @Autowired
    private SysDeptDao sysDeptDao;
    @Autowired
    private TaskSendDao taskSendDao;

    @Autowired
    private TaskNewDao taskNewDao;

    @Autowired
    private SysUserDao userDao;

    @Autowired
    private TaskMessageService taskMessageService;

    @Autowired
    private TaskLogService taskLogService;


    @Override
    protected BaseDao<TaskBack, String> getDao() {
        return taskBackDao;
    }

    @Override
    protected void beforeSave(TaskBack obj) {
        obj.setSysId(tableService.updateMaxSysId("t_task_back", null));
    }

    public List<TaskBack> getBySendId(String id) {
        return taskBackDao.getBySendId(id);
    }

    SimpleDateFormat simdate=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    /**
     * 反馈问题保存,新增消息
     *
     * @param taskBack
     * @return
     */
    public String saveBack(TaskBack taskBack) {
        String userId = SysUserUtils.getUserId();
        SysUser user = userDao.getById(userId);
        SysDept dept = sysDeptDao.getById(SysUserUtils.getDeptId());
        String taskId = taskSendDao.getById(taskBack.getSendId()).getTaskId();
        TaskNew taskNew = taskNewDao.getById(taskId);
        this.save(taskBack);

        //每次反馈的时候添加一条消息提醒
        TaskMessage taskMessage = new TaskMessage();
        taskMessage.setTaskId(taskId);
        taskMessage.setSendId(taskBack.getSendId());//派发id
        taskMessage.setBackId(taskBack.getSysId());  //反馈表id
        taskMessage.setState("0");//设置消息未读
        taskMessage.setType("1"); //设置消息类型为反馈
        taskMessage.setSenderId(userId) ;  //消息反馈人的id
        taskMessage.setAccepterId(taskNew.getCreateUser());  //消息接收人id
        taskMessage.setSendDeptName(dept.getDeptName());//反馈单位名
        taskMessage.setContent(taskBack.getContent());  //设置消息内容
        taskMessage.setTaskName(taskNew.getTaskName());//任务名
        taskMessageService.save(taskMessage);

        taskBack.setUserId(userId);

        TaskSend taskSend = taskSendDao.getById(taskBack.getSendId());
        taskSend.setType("2");
        taskSendDao.update(taskSend);


        //任务流程记录
        TaskLog taskLog = new TaskLog();
        taskLog.setProcesses("任务反馈");
        taskLog.setTableId(taskSend.getTaskId());
        taskLog.setTransactor(taskSend.getDepartName()+user.getName());
        Date date =new Date();
        String datetime=simdate.format(date);
        taskLog.setDatetime(datetime);
        taskLog.setTransactorTel(user.getMobile());
        taskLogService.save(taskLog);

        //保存完后判断当前任务下的所有派发是否都已经签收
        //当前任务下派发的任务状态都是待反馈的send没有数据,表示都已反馈,改变主任务的type
        List<TaskSend> sendList = taskSendDao.queryByTaskIdAndType(taskSend.getTaskId(), "2");
        if (sendList.size() <= 0) {
            TaskNew taskNew1 = taskNewDao.getById(taskSend.getTaskId());
            taskNew1.setType("2");
            taskNewDao.update(taskNew1);
        }
        return "200";
    }

    /**
     * 审批/回复
     * @param taskBack
     * @return
     */
    public String updateBack(TaskBack taskBack) {
        TaskBack newTaskBack = taskBackDao.getById(taskBack.getSysId());
        newTaskBack.setResult(taskBack.getResult());
        taskBackDao.update(newTaskBack);

        TaskSend taskSend = taskSendDao.getById(taskBack.getSendId());
        TaskNew taskNew = taskNewDao.getById(taskSend.getTaskId());
        SysUser user = userDao.getById(SysUserUtils.getUserId());
        SysDept dept = sysDeptDao.getById(SysUserUtils.getDeptId());

        //每次反馈的时候添加一条消息提醒
        TaskMessage taskMessage = new TaskMessage();
        taskMessage.setTaskId(taskSend.getTaskId());
        taskMessage.setSendId(taskBack.getSendId());//派发id
        taskMessage.setBackId(taskBack.getSysId());  //反馈表id
        taskMessage.setState("0");//设置消息未读
        taskMessage.setType("1"); //设置消息类型为反馈
        taskMessage.setSenderId(user.getUserId()) ;  //消息回复人的id
        taskMessage.setAccepterId(taskSend.getUserId());  //消息接收人id
        taskMessage.setSendDeptName(dept.getDeptName());//回复单位名
        taskMessage.setContent(taskBack.getResult());  //设置消息内容
        taskMessage.setTaskName(taskNew.getTaskName());//任务名
        taskMessageService.save(taskMessage);
        //任务流程记录
        TaskLog taskLog = new TaskLog();
        taskLog.setProcesses("反馈消息批复");
        taskLog.setTableId(taskSend.getTaskId());
        taskLog.setTransactor(user.getName());
        Date date =new Date();
        String datetime=simdate.format(date);
        taskLog.setDatetime(datetime);
        taskLog.setTransactorTel(user.getMobile());
        taskLogService.save(taskLog);

        return "200";
    }
}
